#!/usr/bin/env python

from mxit import handles
import re
import mimetypes
import time
import gettext
import os

gettext.install('blendIT','locale');

# Outgoing messages that need some extra processing
# before they get sent.
class mxitmessages:
   def __init__(self,blend):
      self.blend = blend;

   def sendCommand(self,jid,message,display=True):
      buddy = self.blend.conn.roster.get_buddy(jid);
      if buddy is None:
         buddy = self.blend.conn.roster.get_room(jid);
      if buddy is None:
         return; # Error!

      flags = self._flags(message);
      if display:
         self._addToConversation(jid,message,flags);
      self.blend.conn.message(buddy,message,type=handles.MessageType.COMMAND,flags=flags);
      

   def sendMessage(self,jid,message,display=True):
      buddy = self.blend.conn.roster.get_buddy(jid);
      if buddy is None:
         buddy = self.blend.conn.roster.get_room(jid);
      if buddy is None:
         return; # Error!

      flags = self._flags(message);
      if display:
         self._addToConversation(jid,message,flags);
      self.blend.conn.message(buddy,message,flags=flags);

   def sendEncryptedMessage(self,jid,message):
      buddy = self.blend.conn.roster.get_buddy(jid);
      flags = self._flags(message);
      conv = self.blend.gui.chatWindow.convs.getByJID(jid);
      key = conv.sendEncryptKey;
      self._addToConversation(jid,message,flags);
      self.blend.conn.encrypted_message(buddy,message,key,flags);
      if conv.rememberKeyOnce == True:
         conv.disableEncryption();
         
         # Change the check box
         n = self.blend.gui.chatWindow.notebook.get_current_page();
         if n == self.blend.gui.chatWindow.convs.getPageNumberFromJID(conv.buddy.jid):
            self.blend.gui.chatWindow.encryptMenuItem.set_active(False);

   def sendFile(self,jid,description,path):
      filename = os.path.basename(path);
      size = os.path.getsize(path);
      rfd = open(path,'rb');

      # Get the mimetype
      mime = mimetypes.guess_type(filename)[0];
      mimetypes.add_type('application/mxit-msgs','.mxm');
      mimetypes.add_type('application/mxit-emo','.mxf');
      if mime == None:
         mime = "application/octet-stream";

      fd = handles.FileDescriptor(0,size,filename,mime,description);
      print "We are requesting a buddy with jid", jid;
      buddy = self.blend.conn.roster.get_buddy(jid);
      if buddy is None:
         if self.blend.config.debug:
            print "Not technically possible", jid;
         return;

      self.blend.conn.send_file(fd,buddy,rfd);
      rfd.close();
      
      self.blend.gui.chatWindow.systemMessage(_("Sent file %s") % filename,jids=[jid]);


   def _addToConversation(self,jid,message,flags):
      conv = self.blend.gui.chatWindow.convs.getByJID(jid);
      name = self.blend.currentAccount.alias;
      msg = handles.Message(name,message,time.time(),flags=flags);
      self.blend.gui.chatWindow.parseMessage(msg,conv);

   # We must search for markup and custom emoticons in the message
   # and add in the appropriate flags for them.
   def _flags(self,message):
      flags = [];
      
      # Custom Emoticons
      pattern = r"(\.\{[a-zA-Z0-9]*\})";
      pattern = re.compile(pattern,re.MULTILINE);
      if pattern.search(message) is not None:
         flags.append(handles.MessageFlags.CUSTOM_EMOTICONS);
      
      flags.append(handles.MessageFlags.MARKUP);

      return flags;